package com.antcom.antread.antreaddatacrawl.himalayaData.service.impl;

import com.antcom.antread.antreaddatacrawl.himalayaData.dao.BookInfoDao;
import com.antcom.antread.antreaddatacrawl.himalayaData.dao.repository.BCmiddleRepository;
import com.antcom.antread.antreaddatacrawl.himalayaData.dao.repository.BookInfoRepository;
import com.antcom.antread.antreaddatacrawl.himalayaData.dao.repository.ChapterInfoRepository;
import com.antcom.antread.antreaddatacrawl.himalayaData.entity.BCmiddle;
import com.antcom.antread.antreaddatacrawl.himalayaData.entity.BookInfo;
import com.antcom.antread.antreaddatacrawl.himalayaData.entity.ChapterInfo;
import com.antcom.antread.antreaddatacrawl.himalayaData.service.BookInfoService;
import com.antcom.antread.antreaddatacrawl.himalayaData.utils.SendRequest;
import com.antcom.antread.common.core.exception.CustomException;
import com.antcom.antread.common.core.text.UUIDUtil;
import com.antcom.antread.common.core.utils.encryptUtil.URLUtil;
import com.antcom.antread.common.core.utils.fileRead.FileUtils;

import javax.inject.Inject;
import javax.inject.Named;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

/**
 * 业务处理层：BookInfoServiceImpl
 *
 * @Author 中国蚂蚁技术有限公司
 * @create 2020/12/1 19:08
 */
@Named
public class BookInfoServiceImpl implements BookInfoService {

    @Inject
    private BookInfoRepository bookInfoRepository;

    @Inject
    private ChapterInfoRepository chapterInfoRepository;

    @Inject
    private BCmiddleRepository bCmiddleRepository;

    @Inject
    private BookInfoDao bookInfoDao;

    /**
     * 下载有声书信息
     *
     * @return
     */
    @Override
    public void downBookInfo(String bookName, String condition, String page) {
        List<BookInfo> bookInfos = SendRequest.searchBOOK(bookName, condition, page);
        if (bookInfos == null || bookInfos.size()==0) {
            throw new CustomException("书籍信息未获取到");
        }
        for (BookInfo bookInfo : bookInfos) {
            List<BookInfo> byBookName = bookInfoDao.findByBookName(bookInfo.getBookName());
            if (byBookName.size()>0){
                continue;
            }
            bookInfoRepository.save(bookInfo);
        }
    }

    @Override
    public void downChapterInfo(String id) {
        Optional<BookInfo> bookInfo = bookInfoRepository.findById(id);
        bookInfo.ifPresent(t -> {
            String himalayaId = t.getHimalayaId();
            System.out.println(himalayaId);
            List<ChapterInfo> list = SendRequest.searchChapter(himalayaId);
            for (ChapterInfo chapterInfo : list) {
                chapterInfoRepository.save(chapterInfo);
            }
        });

    }

    @Override
    public void downBcMiddle(String id, String sign) {
        BookInfo bookInfo = bookInfoRepository.getOne(id);
        List<ChapterInfo> chapterInfos = chapterInfoRepository.findByChapterSign(sign);
        for (ChapterInfo chapterInfo : chapterInfos) {
            BCmiddle bCmiddle = new BCmiddle();
            bCmiddle.setId(UUIDUtil.getUUID());
            bCmiddle.setBookId(bookInfo.getBookId());
            bCmiddle.setChaperId(chapterInfo.getChapterId());
            bCmiddleRepository.save(bCmiddle);
        }
    }

    @Override
    public void downAudio(String sign, Integer start, Integer end) {
        List<ChapterInfo> chapterInfos = chapterInfoRepository.findListChapter(sign, start, end);
        for (ChapterInfo chapterInfo : chapterInfos) {
            Integer himalayaChId = chapterInfo.getHimalayaChId();
            String audioUrl = SendRequest.searchAudio(himalayaChId);
            chapterInfo.setChapterVoice(audioUrl);
            chapterInfo.setChapterSign("1");
            chapterInfoRepository.save(chapterInfo);
        }
    }

    @Override
    public Optional<BookInfo> findBook(String id) {
        Optional<BookInfo> bookInfo = bookInfoDao.findOne(id);
        return bookInfo;
    }

    /**
     * 用户搜索接口实现
     *
     * @param bookName
     * @return
     */
    @Override
    public List<BookInfo> getBook(String bookName) {
        List<BookInfo> book = bookInfoDao.findByBookName(bookName);
        if (book.size() == 0 || book == null) {
            throw new CustomException("暂时没有该书籍哦,在下载栏中下载试试吧");
        }
        return book;
    }


    /**
     * 获取本地路径保存数据库
     * 出现异常立即回滚
     *
     * @param dirPath  himalaya/丰乳肥臀 格式
     * @param filePath 文件路径
     * @param sign     标识
     */
    @Override
    @Transactional(rollbackOn = Exception.class)
    public void downLocalUrl(String filePath, String dirPath, String sign) {
        List<String> fileName = FileUtils.getFileName(filePath);
        fileName.forEach(t -> {
            String dirUrl = URLUtil.URLEn("/antRead/audio/" + dirPath + "/" + t) + ".m4a";
            ChapterInfo chapterInfo = chapterInfoRepository.findByChapterNameLikeAndChapterSign(t, sign);
            chapterInfo.setChapterVoice(dirUrl);
            chapterInfo.setChapterSign("1");
            chapterInfoRepository.save(chapterInfo);
        });
    }


}

